﻿using And.Shp.Common.Users;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using SharepointCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Uportal.Shp.Core.Common
{
    /// <summary>
    /// Вспомогательный класс для отправки писем
    /// </summary>
    public class MailHelper
    {
        /// <summary>
        /// Строка вида <a href='AUrl' target='_blank'>ATitle</a>
        /// </summary>
        public static string Anchor(string url, string title)
        {
            return "<a href='" + url + "' target='_blank'>" + title + "</a>";
        }

        /// <summary>
        /// Отправка почты на адрес AD
        /// </summary>
        public static void SendEmail(SPWeb web, string email, string subject, string body)
        {
            if (!string.IsNullOrEmpty(body))
                body = body.Replace("\n", "<BR/>");
            SPUtility.SendEmail(web, false, false, email, subject, body);
        }

        /// <summary>
        /// Отправка почты пользователю
        /// </summary>
        public static void SendEmail(SPWeb web, User user, string subject, string body)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            var userWr = new UserWrapper(web, user.Id);
            if (!string.IsNullOrEmpty(body))
                body = body.Replace("\n", "<BR/>");
            SPUtility.SendEmail(web, false, false, userWr.Email, subject, body);
        }
    }
}
